package com.lanmei.group.weixin.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;

import com.jfinal.kit.PropKit;




/**
 * 
 * ImageMagick和im4java处理图片
 * 
 * @author sunlightcs
 * 
 *         2011-6-1
 * 
 *         http://hi.juziku.com/sunlightcs/
 */

public class ImageUtil {

	/**
	 * 
	 * ImageMagick的路径
	 */

	public static String imageMagickPath = null;

	static {

		/**
		 * 获取ImageMagick的路径
		 */

		// Properties prop = new PropertiesFile().getPropertiesFile();

		// linux下不要设置此值，不然会报错
		imageMagickPath = PropKit.use("config.properties").get("imageMagickPath");
		//imageMagickPath = "C:/Program Files (x86)/ImageMagick-6.8.8-Q8";
	}

	/**
	 * 
	 * 根据坐标裁剪图片
	 * 
	 * @param srcPath
	 *            要裁剪图片的路径
	 * @param newPath
	 *            裁剪图片后的路径
	 * @param x
	 *            起始横坐标
	 * @param y
	 *            起始挫坐标
	 * @param x1
	 *            结束横坐标
	 * @param y1
	 *            结束挫坐标
	 */

	public static void cutImage(String srcPath, String newPath, int x, int y,
			int x1, int y1) throws Exception {
		int width = x1 - x;
		int height = y1 - y;
		IMOperation op = new IMOperation();
		op.addImage(srcPath);

		/**
		 * width：裁剪的宽度 height：裁剪的高度 x：裁剪的横坐标 y：裁剪的挫坐标
		 */

		op.crop(width, height, x, y);
		op.addImage(newPath);
		ConvertCmd convert = new ConvertCmd();
		// linux下不要设置此值，不然会报错
		if (isWindows()) {
			convert.setSearchPath(imageMagickPath);
		}
		convert.run(op);
	}

	/**
	 * 根据宽度和高度缩放图片
	 * 
	 * @param width
	 *            缩放后的图片宽度
	 * @param height
	 *            缩放后的图片高度
	 * @param srcPath
	 *            源图片路径
	 * @param newPath
	 *            缩放后图片的路径
	 */

	public static void cutImage(int width, int height, String srcPath,
			String newPath) {
		if (isInSize(new File(srcPath),width,height)) { //若不需要缩放,则直接复制文件到对应目录
			try {
				FileUtil.copy(new File(srcPath), new File(newPath));
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		IMOperation op = new IMOperation();
		op.addImage(srcPath);
		op.resize(width, height);
		op.addImage(newPath);
		ConvertCmd convert = new ConvertCmd(false);
		System.out.println(op);
		// linux下不要设置此值，不然会报错
		if (isWindows()) {
			convert.setSearchPath(imageMagickPath);
		}
		try {
			convert.run(op);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (IM4JavaException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * 根据宽度缩放图片
	 * @param width
	 *            缩放后的图片宽度
	 * @param srcPath
	 *            源图片路径
	 * @param newPath
	 *            缩放后图片的路径
	 */

	public static void cutImage(int width, String srcPath, String newPath)
			{
		if (isInSize(new File(srcPath),width,-1)) { //若不需要缩放,则直接复制文件到对应目录
			try {
				FileUtil.copy(new File(srcPath), new File(newPath));
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		IMOperation op = new IMOperation();
		op.addImage(srcPath);
		op.resize(width, null);
		op.addImage(newPath);
		ConvertCmd convert = new ConvertCmd();

		// linux下不要设置此值，不然会报错
		if (isWindows()) {
			convert.setSearchPath(imageMagickPath);
		}
		try {
			convert.run(op);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IM4JavaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * 给图片加水印
	 * 
	 * @param srcPath
	 *            源图片路径
	 */

	public static void addImgText(String srcPath,String text) throws Exception {
		IMOperation op = new IMOperation();
		op.font("宋体").gravity("southeast").pointsize(18).fill("#BCBFC8").draw(text);
		op.addImage();
		op.addImage();
		ConvertCmd convert = new ConvertCmd();

		// linux下不要设置此值，不然会报错
		if (isWindows()) {
			convert.setSearchPath(imageMagickPath);
		}
		convert.run(op, srcPath, srcPath);

	}

	/**
	 * 判断图片文件的大小是否超出指定要转换的大小
	 * @param file
	 * @param signWidth
	 * @param signHeight
	 * @return
	 * @throws IOException
	 */
	public static boolean isInSize(File file, int signWidth, int signHeight)
			{
		BufferedImage bi;
		try {
			bi = ImageIO.read(file);
			if (bi==null) {
				return false;
			}
			if (signWidth==-1 && signHeight==-1) {
				return false;
			} else if (signWidth==-1 && signHeight>0) {
				return bi.getHeight()<signHeight;
			} else if (signWidth>0 && signHeight==-1) {
				return bi.getWidth()<signWidth;
			} else if (bi.getHeight() > signHeight || bi.getWidth() > signWidth) {
				return false;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	/**
	 * 当前系统是否是windows
	 * @return
	 */
	private static boolean isWindows() {
		String osName = System.getProperty( "os.name" );  //操作系统名称
		if (osName==null || osName.length()==0) {
			return false;
		} else if (osName.indexOf("Windows")!=-1) {
			return true;
		} else {
			return false;
		}
	}

	public static void main(String[] args) throws Exception {

		//cutImage("E:/testphoto/a.jpg", "E:/testphoto/a1.jpg", 98, 48, 370,320);

		 cutImage(600,600, "F:/photos/avatar.jpeg", "F:/photos/a1.jpg");

		//cutImage(400, 600, "E:/testphoto/a.jpg", "E:/testphoto/a2.jpg");

		//addImgText("E:/testphoto/a2.jpg","test");

	}

}
